{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 线性回归处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = np.loadtxt('data/Abalone.csv', delimiter = \",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 引入模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import mean_absolute_error\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.model_selection import cross_val_predict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 使用sklearn完成多元线性回归的十折交叉验证验证"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LinearRegression()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 选取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.      0.455   0.365  ...  0.2245  0.101   0.15  ]\n",
      " [-1.      0.35    0.265  ...  0.0995  0.0485  0.07  ]\n",
      " [ 1.      0.53    0.42   ...  0.2565  0.1415  0.21  ]\n",
      " ...\n",
      " [-1.      0.6     0.475  ...  0.5255  0.2875  0.308 ]\n",
      " [ 1.      0.625   0.485  ...  0.531   0.261   0.296 ]\n",
      " [-1.      0.71    0.555  ...  0.9455  0.3765  0.495 ]]\n"
     ]
    }
   ],
   "source": [
    "x = data[:,0:-1]\n",
    "y = data[:,-1]\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4177,)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prediction = cross_val_predict(model, x, y, cv = 10)\n",
    "prediction.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.67\n",
      "2.31\n",
      "5.33\n"
     ]
    }
   ],
   "source": [
    "mae = round(mean_absolute_error(y, prediction),2)\n",
    "rmse = round(mean_squared_error(y, prediction) ** 0.5,2)\n",
    "mse = round(mean_squared_error(y, prediction),2)\n",
    "print(mae)\n",
    "print(rmse)\n",
    "print(mse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 线性回归\n",
    "多元线性回归，十折交叉验证指标\n",
    "\n",
    "MAE|RMSE|MSE\n",
    "-|-|-\n",
    "1.67|2.31|5.33"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
